/**
  ******************************************************************************
  * File Name          : taskctrl.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include "ctrl_common.h"
#include "syscfg.h"
#include "dwlcd.h"
#include "route.h"
#include <string.h>
/*------------ task define ------------*/

TaskHandle_t xRunDog_Thd = NULL;
StaticTask_t xRunDog_Tcb;
StackType_t  xRunDog_Stk[ RUNDOG_STACK_SIZE ];

TaskHandle_t xLedScreen_Thd = NULL;
StaticTask_t xLedScreen_Tcb;
StackType_t  xLedScreen_Stk[ LEDSCREEN_STACK_SIZE ];

/**
  * @brief  Create task
  * @param  None
  * @retval None
  */
void APP_CreateTask(void)
{
	xRunDog_Thd  = xTaskCreateStatic(vRunDog_Task, \
	                                 "RunD", \
	                                 RUNDOG_STACK_SIZE, \
	                                 (void *)1, \
									                 RUNDOG_TSK_PRIORITY, \
									                 xRunDog_Stk, \
									                 &xRunDog_Tcb);

	xTerminal_Thd  = xTaskCreateStatic(vTerminal_Task,
																	   "term",
																	   TTY_STACK_SIZE,
																	   (void *)1,
																	   TTY_TSK_PRIORITY,
																	   xTerminal_Stk,
																	   &xTerminal_Tcb);

	xStoragePara_Thd = xTaskCreateStatic(vStoragePara_Task, \
										                   "para", \
																	     STGPARA_STACK_SIZE, \
																	     (void *)1, \
																	     STGPARA_TSK_PRIORITY, \
																	     xStoragePara_Stk, \
																	     &xStoragePara_Tcb);

	xDwLcdRx_Thd   = xTaskCreateStatic(vDwlcdRecv_Task,
																	   "dwRx",
																	   DWLCDRX_STACK_SIZE,
																	   (void *)1,
																	   DWLCDRX_TSK_PRIORITY,
																	   xDwLcdRx_Stk,
																	   &xDwLcdRx_Tcb);

	xRoute_Thd = xTaskCreateStatic(vRoute_Task, \
																 "rout", \
															   ROUTE_STACK_SIZE, \
																 (void *)1, \
																 ROUTE_TSK_PRIORITY, \
																 xRoute_Stk, \
																 &xRoute_Tcb);

  xRoute_485_Thd = xTaskCreateStatic(vRoute_485_Task, \
                                     "r485", \
                                     ROUTE_485_STACK_SIZE, \
                                     (void *)1, \
                                     ROUTE_485_TSK_PRIORITY, \
                                     xRoute_485_Stk, \
                                     &xRoute_485_Tcb);

  xLedScreen_Thd = xTaskCreateStatic(vLedScreen_Task, \
                                     "leds", \
                                     LEDSCREEN_STACK_SIZE, \
                                     (void *)1, \
                                     LEDSCREEN_TSK_PRIORITY, \
                                     xLedScreen_Stk, \
                                     &xLedScreen_Tcb);

	xNetWorkRecv_Thd= xTaskCreateStatic(vNetWorkReceive_Task,
																	 "entrcv",
																	 NETWORK_RECV_STACK_SIZE,
																	 (void *)1,
																	 NETWORK_RECV_TSK_PRIORITY,
																	 xNetWorkRecv_Stk,
																	 &xNetWorkRecv_Tcb);
																	 
#if(COLLECTOR_USE_TYPE == COLLECTOR_USE_LORA)
	xCollertor_Thd = xTaskCreateStatic(vCollertor_Task,
																	  "LoRa",
																	  COLLERTOR_STACK_SIZE,
																	  (void *)1,
																	  COLLERTOR_TSK_PRIORITY,
																	  xCollertor_Stk,
																	  &xCollertor_Tcb);
#endif
	
	xAdcNtc_Thd = xTaskCreateStatic(vAdcNtc_Task,
																	"adnt",
																	ADCNTC_STACK_SIZE,
																	(void *)1,
																	ADCNTC_TSK_PRIORITY,
																	xAdcNtc_Stk,
																	&xAdcNtc_Tcb);
	Control_CreateTask();
}

void vRunDog_Task( void * pvParameters )
{
	uint32_t step = 0;
	uint8_t  rl_day = 0;
  
	Sys_GetDataTime(&Sys_Runtime);
	rl_day = Sys_Runtime.day;
  
  vTaskDelay(2500);
  
  DW_Send_ec_mode();
  DW_Send_ec_para_jt();
  DW_Send_ec_para_dl();
  DW_Send_ec_para_sk();
  DW_Send_ec_para_td();
  DW_Send_rilcl();
  DW_Send_devinfo();
  
	while(1)
	{
		LED1_Toggle();
    
    /* 每 5 分钟 上传一次周期数据,并判断日龄 */
		if(step > 300)
		{
			step = 0;
			Route_ec_tonet_collector();
			Route_ec_tonet_md();
      
      if(rl_day != Sys_Runtime.day)
			{
				rl_day = Sys_Runtime.day;
				
        Route_gw_tonet_time();

        Route_485_broadtime();

				SysRil_RunPara.ril = SysRil_Day_diff(SysRil_RunPara.year, SysRil_RunPara.month, SysRil_RunPara.day, \
                                             Sys_Runtime.year, Sys_Runtime.month, Sys_Runtime.day);
        
        DW_Send_rilcl();
      }
		}
		
		vTaskDelay(1000);
		
    /* 每 1 秒 更新一下系统时间 */
    Sys_GetDataTime(&Sys_Runtime);
    
    /* 每 1 秒 复位一次看门狗 */
    fwdgt_counter_reload();
    
		step++;
	}
}

extern void LED_SetIint(void);
extern void LED_SetScreen1(void);

void vLedScreen_Task( void * pvParameters )
{
  LED_SetIint();
  
  while(1)
  {
    LED_SetScreen1();

    vTaskDelay(5000);
  }
}

/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
  *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
  *ppxIdleTaskStackBuffer = &xIdleStack[0];
  *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}

/* Private application code --------------------------------------------------*/

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
